return function(callback)
  local conf = dofile('conf/wifi.lc')

  if conf.mode == wifi.STATION and conf.sta.ssid then
    wifi.setmode(wifi.STATION)
    wifi.sta.config(conf.sta)
    print('Client MAC: ',wifi.sta.getmac())

    wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(args)
      print("IP: " .. args["IP"])
      wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(args)
        print("Lost connectivity! Restarting...")
        node.restart()
      end)
      callback(wifi.getmode(), args["IP"])
    end)

    -- What if after a while (30 seconds) we didn't connect? Restart and keep trying.
    local watchdogTimer = tmr.create()
    watchdogTimer:register(30000, tmr.ALARM_SINGLE, function (watchdogTimer)
      if wifi.sta.getip() == nil then
        print("No IP after a while. Restarting...")
        node.restart()
      else
        watchdogTimer:unregister()
        watchdogTimer = nil
        collectgarbage()
      end
    end)
    watchdogTimer:start()
    watchdogTimer = nil
  else
    wifi.setmode(wifi.STATIONAP)
    wifi.ap.config({ssid=conf.ap.ssid,pwd=conf.ap.pwd})
    wifi.ap.setip({ip=conf.ap.ip,netmask=conf.ap.netmask,gateway=conf.ap.gateway})
    print('AP MAC: ',wifi.ap.getmac())
  end

  conf = nil
  collectgarbage()

  if (wifi.getmode() ~= wifi.STATION) then
    print("IP: " .. wifi.ap.getip())
    callback(wifi.getmode(), wifi.ap.getip())
  end
end